package com.lg.partition13;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2022/4/5 9:38
 */
public class P1262间谍网络 {

    private static final int N = 3010, M = 16010, INF = 0x3f3f3f3f;
    private static int[] h = new int[N], hc = new int[N], e = new int[M], ne = new int[M], hw = new int[N], hcw = new int[N];
    private static int[] dfn = new int[N], low = new int[N], stk = new int[N], scc = new int[N], din = new int[N];
    private static boolean[] st = new boolean[N];
    //key为sccId，value为原节点编号
    private static Map<Integer, List<Integer>> map = new HashMap<>(N);
    private static int n, m, idx = 0, timestamp = 0, sccId = 0, top = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        int t = Integer.parseInt(reader.readLine());
        for (int i = 1; i <= n; i++) {
            h[i] = hc[i] = -1;
            hcw[i] = INF;
        }
        while (t-- > 0) {
            String[] ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            hw[a] = b;
        }
        m = Integer.parseInt(reader.readLine());
        for (int i = 0; i < m; i++) {
            String[] ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            add(h, a, b);
        }
        resolve();
        writer.flush();
    }

    private static void resolve() throws IOException {
        for (int i = 1; i <= n; i++) {
            if (dfn[i] == 0) tarjan(i);
        }
        for (int u = 1; u <= n; u++) {
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (scc[u] == scc[v]) continue;
                add(hc, scc[u], scc[v]);
                din[scc[v]]++;
            }
        }
        //如果存在入度为0的节点不能被收买，则不能控制全部节点
        int ans = 0;
        boolean flag = true;
        for (int i = 1; i <= sccId; i++) {
            if (din[i] == 0) {
                ans += hcw[i];
                if (hcw[i] == INF) {
                    flag = false;
                    ans = i;
                    break;
                }
            }
        }
        if (!flag) {
            writer.write("NO\n");
            //该分量的节点必定是一个节点
            writer.write(map.get(ans).get(0) + "\n");
            return;
        }
        writer.write("YES\n");
        writer.write(ans + "\n");
    }

    private static void tarjan(int u) {
        dfn[u] = low[u] = ++timestamp;
        stk[++top] = u;
        st[u] = true;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (dfn[v] == 0) {
                tarjan(v);
                low[u] = Math.min(low[u], low[v]);
            } else if (st[v]) {
                low[u] = Math.min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            sccId++;
            int v = -1;
            List<Integer> ls = new ArrayList<>();
            do {
                v = stk[top--];
                st[v] = false;
                scc[v] = sccId;
                if (hw[v] != 0) hcw[sccId] = Math.min(hcw[sccId], hw[v]);
                ls.add(v);
            } while (u != v);
            map.put(sccId, ls);
        }
    }

    private static void add(int[] h, int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
